<template>
  <FlowBasicListModel
    :loading="loading"
    :flowTemplate="flowTemplate"
    :callback="getTemplateDefinePage"
    title="申请流程"
    v-bind="$attrs"
  />
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { FlowBasicListModel } from '/@/components/FlowBasicListModel';
  import { getTemplateDefinePageApi } from '/@/api/flow/flow';
  import { FlowTemplateResult } from '/@/api/flow/model';
  import { getRowKeys, getRealApi } from '/@/components/ModelTable/src/hooks/functionSet';
  import { useComplexContext } from '../useComplexContext';
  const loading = ref(false);
  const flowTemplate = ref<FlowTemplateResult[]>([]);
  const props = defineProps({
    btnItem: {
      type: Object,
      default: () => {},
    },
  });
  const { record } = useComplexContext();

  async function getTemplateDefinePage() {
    let res: FlowTemplateResult[] = [];
    if (props.btnItem.apiCode) {
      res = await getRealApi({
        params: record,
        apiCode: props.btnItem.apiCode,
      });
    } else {
      let params = getRowKeys(props.btnItem.dynamicParams, record as Recordable<any>);
      let obj = {};
      for (let i in params) {
        obj[i] = params[i]?.value || params[i];
      }

      loading.value = true;
      res = await getTemplateDefinePageApi(obj);
    }

    flowTemplate.value = res.filter((item) => !!item.list && item.list.length);
    loading.value = false;
  }
</script>
<style lang="less" scoped></style>
